//
//  NSImage+WCExtensions.m
//  WabbitCode
//
//  Created by William Towe on 10/17/08.
//  Copyright 2008 Revolution Software. All rights reserved.
//

#import "NSImage+WCExtensions.h"
#import <QuartzCore/CIFilter.h>
#import <QuartzCore/CIVector.h>


@implementation NSImage (WCExtensions)
- (NSImage *)unsavedIconFromImage; {
	NSImage *returnImage = [[[NSImage alloc] initWithSize:[self size]] autorelease];
	NSArray *array = [NSBitmapImageRep imageRepsWithData:[self TIFFRepresentation]];
	for (id item in array) {
		CIImage *coreImage = [[[CIImage alloc] initWithBitmapImageRep:item] autorelease];
		
		CIFilter *filter1 = [CIFilter filterWithName:@"CIColorControls"]; 
		[filter1 setDefaults]; 
		[filter1 setValue:coreImage forKey:@"inputImage"];  
		[filter1 setValue:[NSNumber numberWithFloat:-0.4] forKey:@"inputBrightness"];
		
		/*
		 CIFilter *filter2 = [CIFilter filterWithName:@"CISepiaTone"]; 
		 [filter2 setDefaults]; 
		 [filter2 setValue:[filter1 valueForKey:@"outputImage"] forKey:@"inputImage"];  
		 [filter2 setValue:[NSNumber numberWithFloat:0.9] forKey:@"inputIntensity"];
		 */
		CIImage *result = [filter1 valueForKey:@"outputImage"];
		
		[returnImage addRepresentation:[NSCIImageRep imageRepWithCIImage:result]];
	}
	
	return returnImage;
}

- (NSImage *)badgedImageWithImage:(NSImage *)badge originalSize:(NSSize)originalSize badgeSize:(NSSize)badgeSize; {
	NSImage *image = [[NSImage alloc] initWithSize:originalSize];
	//NSImage *cBadge = [[badge copy] autorelease];
	if (!NSEqualSizes(NSZeroSize, originalSize))
		[self setSize:originalSize];
	if (!NSEqualSizes(NSZeroSize, badgeSize))
		[badge setSize:badgeSize];
	
	[image lockFocus];
	[self compositeToPoint:NSZeroPoint operation:NSCompositeCopy];
	[badge compositeToPoint:NSMakePoint([self size].width-[badge size].width, 0) operation:NSCompositeSourceOver];
	[image unlockFocus];
	
	return [image autorelease];
}
@end
